package com.macasa.panel.common.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class Logger
 * <P>
 * Surcouche de la classe org.apache.commons.logging.Log
 * 
 * @author Alexis Sorbier
 * @date 19 mai 2011
 */
public class Logger {

	/** Log log */
	protected static Log	log;

	/**
	 * Logger un message LEVEL: DEBUG
	 * <P>
	 * 
	 * @param String
	 *            message
	 */
	public static void debug(final String message) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isDebugEnabled()) {
			Logger.log.debug(message);
		}
	}

	/**
	 * Logger un message LEVEL: DEBUG
	 * <P>
	 * 
	 * @param String
	 *            message
	 * @param Throwable
	 *            t
	 */
	public static void debug(final String message, final Throwable t) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isDebugEnabled()) {
			Logger.log.debug(message, t);
		}
	}

	/**
	 * Logger un message LEVEL: ERROR
	 * <P>
	 * 
	 * @param String
	 *            message
	 */
	public static void error(final String message) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isErrorEnabled()) {
			Logger.log.error(message);
		}
	}

	/**
	 * Logger un message LEVEL: ERROR
	 * <P>
	 * 
	 * @param String
	 *            message
	 * @param Throwable
	 *            t
	 */
	public static void error(final String message, final Throwable t) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isErrorEnabled()) {
			Logger.log.error(message, t);
		}
	}

	/**
	 * Logger un message LEVEL: FATAL
	 * <P>
	 * 
	 * @param String
	 *            message
	 */
	public static void fatal(final String message) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isFatalEnabled()) {
			Logger.log.fatal(message);
		}
	}

	/**
	 * Logger un message LEVEL: FATAL
	 * <P>
	 * 
	 * @param String
	 *            message
	 * @param Throwable
	 *            t
	 */
	public static void fatal(final String message, final Throwable t) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isFatalEnabled()) {
			Logger.log.fatal(message, t);
		}
	}

	/**
	 * Retourne la classe appelant le log
	 * <P>
	 * 
	 * @return Class clazz
	 */
	private static Class<?> getCallingClass() {
		final StackTraceElement[] elements = Thread.currentThread().getStackTrace();
		String className = null;
		String methodName = null;
		for (int i = 0; i < elements.length; i++) {
			methodName = elements[i].getMethodName();
			if ("debug".equals(methodName) || "info".equals(methodName) || "warn".equals(methodName) || "trace".equals(methodName)
					|| "error".equals(methodName) || "fatal".equals(methodName)) {
				className = elements[i + 1].getClassName();
				break;
			}
		}
		Class<?> result = null;
		try {
			result = Class.forName(className);
		} catch (final Exception e) {/* IGNORE */
		}
		return result == null ? Object.class : result;
	}

	/**
	 * Logger un message LEVEL: INFO
	 * <P>
	 * 
	 * @param String
	 *            message
	 */
	public static void info(final String message) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isInfoEnabled()) {
			Logger.log.info(message);
		}
	}

	/**
	 * Logger un message LEVEL: INFO
	 * <P>
	 * 
	 * @param String
	 *            message
	 * @param Throwable
	 *            t
	 */
	public static void info(final String message, final Throwable t) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isInfoEnabled()) {
			Logger.log.info(message, t);
		}
	}

	/**
	 * Logger un message LEVEL: TRACE
	 * <P>
	 * 
	 * @param String
	 *            message
	 */
	public static void trace(final String message) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isTraceEnabled()) {
			Logger.log.trace(message);
		}
	}

	/**
	 * Logger un message LEVEL: TRACE
	 * <P>
	 * 
	 * @param String
	 *            message
	 * @param Throwable
	 *            t
	 */
	public static void trace(final String message, final Throwable t) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isTraceEnabled()) {
			Logger.log.trace(message, t);
		}
	}

	/**
	 * Logger un message LEVEL: WARN
	 * <P>
	 * 
	 * @param String
	 *            message
	 */
	public static void warn(final String message) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isWarnEnabled()) {
			Logger.log.warn(message);
		}
	}

	/**
	 * Logger un message LEVEL: WARN
	 * <P>
	 * 
	 * @param String
	 *            message
	 * @param Throwable
	 *            t
	 */
	public static void warn(final String message, final Throwable t) {
		Logger.log = LogFactory.getLog(Logger.getCallingClass());
		if (Logger.log.isWarnEnabled()) {
			Logger.log.warn(message, t);
		}
	}

	/**
	 * Constructeur
	 * <P>
	 * prive pour les classes ne contenant que des methodes statiques
	 */
	private Logger() {}
}
